;NSIS Modern User Interface
;Start Menu Folder Selection Example Script
;Written by Joost Verburg

;--------------------------------
;Include Modern UI

  !include "MUI.nsh"

;--------------------------------
;General

  ;Name and file
  Name "TinyERP Demo"
  OutFile "tinyerp-demo-setup.exe"

  ;Default installation folder
  InstallDir "$PROGRAMFILES\TinyERP Demo"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\TinyERP Demo" ""

  ;Vista redirects $SMPROGRAMS to all users without this
  RequestExecutionLevel admin

;--------------------------------
;Variables

  Var MUI_TEMP
  Var STARTMENU_FOLDER

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Pages

  !define MUI_ICON ".\demo\pixmaps\tinyerp.ico"
  !define MUI_UNICON ".\demo\pixmaps\tinyerp.ico"
  !define MUI_WELCOMEFINISHPAGE_BITMAP ".\demo\pixmaps\tinyerp-intro.bmp"
  !define MUI_HEADERIMAGE
  !define MUI_HEADERIMAGE_BITMAP ".\demo\pixmaps\tinyerp-header.bmp"

  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_LICENSE ".\demo\\doc\\License.rtf"
 # !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  
  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU" 
  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\TinyERP Demo"
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "TinyERP Demo"
  
  #!insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  
  !insertmacro MUI_PAGE_INSTFILES

  !define MUI_FINISHPAGE_NOAUTOCLOSE
  !define MUI_FINISHPAGE_RUN
  !define MUI_FINISHPAGE_RUN_CHECKED
  !define MUI_FINISHPAGE_RUN_TEXT "Start TinyERP Client"
  !define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
  !insertmacro MUI_PAGE_FINISH

  
  !insertmacro MUI_UNPAGE_WELCOME
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Installer Sections

Section "TinyERP Demo" SecTinyERPDemo

  SetOutPath "$TEMP"
  
  ;ADD YOUR OWN FILES HERE...
  File "server\tinyerp-server-setup.exe"
  File "client\tinyerp-client-setup.exe"
  File "postgresql-8.2-int.msi"

  nsExec::Exec 'net stop tinyerp-service'
  sleep 2
  ExecWait '"$TEMP\tinyerp-server-setup.exe" /S'

  ExecWait '"$TEMP\tinyerp-client-setup.exe" /S'

  ReadRegStr $0 HKLM "System\CurrentControlSet\Control\ComputerName\ActiveComputerName" "ComputerName"
  StrCmp $0 "" win9x
  Goto done
  win9x:
    ReadRegStr $0 HKLM "System\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
  done:
  nsExec::Exec 'net user tinypgsvc /delete'

  Delete "$PROGRAMFILES\\PostgreSQL4TinyERP\\data\\pg_hba.conf"
  StrCpy $1 "0"
  IfFileExists "$PROGRAMFILES\\PostgreSQL4TinyERP\\data\\PG_VERSION" FileExists
  StrCpy $1 "1"
  FileExists:

  ExecWait 'msiexec /i "$TEMP\postgresql-8.2-int.msi" /qb INTERNALLAUNCH=1 ADDLOCAL=server,pgadmin SERVICEDOMAIN="$0" SERVICEACCOUNT="tinypgsvc" SERVICEPASSWORD="tinypgsvcpwd" CREATESERVICEUSER=1 SERVICENAME="PostgreSQL4TinyERP" SUPERUSER="tinypg" SUPERPASSWORD="tinypgpwd" DOINITDB=$1 DOSERVICE=1 LISTENPORT=5432 ENCODING=UTF-8 BASEDIR="$PROGRAMFILES\\PostgreSQL4TinyERP"'

  nsExec::Exec 'net stop pgsql-8.2'
  sleep 2

  SetOutPath "$PROGRAMFILES\\PostgreSQL4TinyERP\\data"
  File "demo\\pgsql\\pg_hba.conf"
  nsExec::Exec 'net start pgsql-8.2'
  sleep 2

  SetOutPath "$INSTDIR"
  ;Store installation folder
  WriteRegStr HKCU "Software\TinyERP Demo" "" $INSTDIR
  
  ;Create uninstaller
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\TinyERP Demo" "DisplayName" "TinyERP Demo (remove only)"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\TinyERP Demo" "UninstallString" "$INSTDIR\Uninstall.exe"
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  

  nsExec::Exec '"$PROGRAMFILES\\TinyERP Server\\tinyerp-server.exe" --stop-after-init --logfile "$PROGRAMFILES\\TinyERP Server\\tinyerp-server.log"  --pg_path="$PROGRAMFILES\\PostgreSQL4TinyERP\\bin" -r tinypg -w tinypgpwd --db_host=127.0.0.1 --db_port=5432 -c "$PROGRAMFILES\\TinyERP Server\\tinyerp-server.conf" -s'
  nsExec::Exec 'net start tinyerp-service'

SectionEnd

;Descriptions

  ;Language strings
  LangString DESC_SecTinyERPServer ${LANG_ENGLISH} "TinyERP Demo."

  ;Assign language strings to sections
  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecTinyERPDemo} $(DESC_SecTinyERPDemo)
  !insertmacro MUI_FUNCTION_DESCRIPTION_END
 
;--------------------------------
;Uninstaller Section

Section "Uninstall"

  Delete "$PROGRAMFILES\\PostgreSQL4TinyERP\\data\\pg_hba.conf"
  RMDir "$PROGRAMFILES\\PostgreSQL4TinyERP\\data"
  RMDir "$PROGRAMFILES\\PostgreSQL4TinyERP"
  ;ADD YOUR OWN FILES HERE...
  Delete "$INSTDIR\Uninstall.exe"
  RMDir "$INSTDIR"
  
  DeleteRegKey HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TinyERP Demo"
  DeleteRegKey /ifempty HKCU "Software\TinyERP Demo"

SectionEnd

Function LaunchLink
  ExecShell "" "$PROGRAMFILES\\TinyERP Client\\tinyerp-client.exe"
FunctionEnd

